package com.breakfast.fun.view.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.breakfast.fun.util.T;
import com.breakfast.fun.R;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;

/**
 * 计算打烊时间控件
 */
public class BookCountDown extends LinearLayout {
    private TextView mHourTv;
    private TextView mMinuteTv;
    private TextView mSecondTv;
    private int hour;
    private int minute;
    private int second;
    private boolean isDown;
    private Timer timer = new Timer();
    private boolean isFinish = false;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (second - 1 < 0) {
                if (minute > 0) {
                    minute--;
                    second = 59;
                } else {
                    if (hour > 0) {
                        hour--;
                        minute = 59;
                        second = 59;
                    } else {
                        second = 0;
                        task.cancel();
                        timer.cancel();
                        timer = null;
                        isFinish = true;
                    }
                }
            } else {
                second--;
            }
            setTimeValue();
        }
    };

    public BookCountDown(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public BookCountDown(Context context) {
        super(context);
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(
                R.layout.widget_book_count_down, null);
        this.mHourTv = (TextView) view.findViewById(R.id.book_count_down_hour);
        this.mMinuteTv = (TextView) view
                .findViewById(R.id.book_count_down_minute);
        this.mSecondTv = (TextView) view
                .findViewById(R.id.book_count_down_second);
        this.addView(view);
    }

    public void setTime(int hour, int minute, int second, boolean isStart) {
        this.hour = hour;
        this.minute = minute;
        this.isDown = isStart;
        this.second = second;
        setTimeValue();
        if (isDown) {
            isFinish = false;
            this.start();
        }
    }

    /**
     * 获得当前剩余时间
     *
     * @return
     */
    public String getTime() {
        return this.mHourTv.getText().toString() + ":" + this.mMinuteTv.getText().toString() + ":" + this.mSecondTv.getText().toString();
    }

    /**
     * 获得时间是否结束（00:00:00）
     *
     * @return
     */
    public boolean getTimeIsComplete() {
        return getTime().equals("00:00:00");
    }

    /**
     * 已打烊直接提示
     *
     * @return
     */
    public boolean getTimeComplete() {
        if (getTimeIsComplete()) {
            T.show("亲，已经过了打烊时间，请明天提早预定哦！");
        }
        return getTime().equals("00:00:00");
    }

    private void setTimeValue() {
        this.mHourTv.setText(dealTime(this.hour));
        this.mMinuteTv.setText(dealTime(this.minute));
        this.mSecondTv.setText(dealTime(this.second));
    }

    public void start() {
        if (timer == null) {
            timer = new Timer();
        }
        timer.schedule(task, 1000, 1000);
    }

    private String dealTime(int value) {
        return value < 10 ? "0" + value : value + "";
    }

    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            Message message = new Message();
            message.what = 1;
            handler.sendMessage(message);
        }
    };

    public boolean getState() {
        return this.isFinish;
    }
}
